package com.ruoyi.coupon.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.coupon.domain.TbCouponType;
import com.ruoyi.coupon.service.ITbCouponTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 优惠券类型Controller
 *
 * @author gzc
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/couponType")
public class TbCouponTypeController extends BaseController {
    @Autowired
    private ITbCouponTypeService tbCouponTypeService;

    /**
     * 查询优惠券类型列表
     */
    @RequiresPermissions(value = {"couponType:couponType:list","coupon:coupon:MyCoupon"},logical = Logical.OR)
    @GetMapping("/list")
    public TableDataInfo list(TbCouponType tbCouponType) {
        startPage();
        List<TbCouponType> list = tbCouponTypeService.selectTbCouponTypeList(tbCouponType);
        return getDataTable(list);
    }

    /**
     * 导出优惠券类型列表
     */
    @RequiresPermissions("couponType:couponType:export")
    @Log(title = "优惠券类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCouponType tbCouponType) {
        List<TbCouponType> list = tbCouponTypeService.selectTbCouponTypeList(tbCouponType);
        ExcelUtil<TbCouponType> util = new ExcelUtil<TbCouponType>(TbCouponType.class);
        util.exportExcel(response, list, "优惠券类型数据");
    }

    /**
     * 获取优惠券类型详细信息
     */
    @RequiresPermissions("couponType:couponType:query")
    @GetMapping(value = "/{couponTypeId}")
    public AjaxResult getInfo(@PathVariable("couponTypeId") Long couponTypeId) {
        return success(tbCouponTypeService.selectTbCouponTypeByCouponTypeId(couponTypeId));
    }

    /**
     * 新增优惠券类型
     */
    @RequiresPermissions("couponType:couponType:add")
    @Log(title = "优惠券类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCouponType tbCouponType) {
        return toAjax(tbCouponTypeService.insertTbCouponType(tbCouponType));
    }

    /**
     * 修改优惠券类型
     */
    @RequiresPermissions("couponType:couponType:edit")
    @Log(title = "优惠券类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCouponType tbCouponType) {
        return toAjax(tbCouponTypeService.updateTbCouponType(tbCouponType));
    }

    /**
     * 删除优惠券类型
     */
    @RequiresPermissions("couponType:couponType:remove")
    @Log(title = "优惠券类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponTypeIds}")
    public AjaxResult remove(@PathVariable Long[] couponTypeIds) {
        return toAjax(tbCouponTypeService.deleteTbCouponTypeByCouponTypeIds(couponTypeIds));
    }
}
